import 'dart:io';

import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:photo_view/photo_view.dart';

import '../../config/public_text.dart';
import '../../model/key_value_model.dart';
import '../../tool/file_manager.dart';
import '../../tool/permission_manager.dart';
import '../../tool/public_tool.dart';
import '../dialog/public_alert_dialog.dart';
import '../indicator/toast.dart';
import '../sheet/action_sheet.dart';

/// FileName show_image_view
///
/// @Author lax
/// @Date 2024/5/28
///
/// @Description

class ShowImageView extends StatelessWidget {
  /// 本地路径
  final String? localPath;

  /// bundle路径
  final String? bundlePath;

  final String? urlString;

  final double? minScale;

  final double? maxScale;

  final bool? canSave;

  const ShowImageView({
    super.key,
    this.localPath,
    this.bundlePath,
    this.urlString,
    this.minScale,
    this.maxScale,
    this.canSave = true,
  });

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ClipRRect(
        child: GestureDetector(
          onLongPress: () async {
            if (canSave != true) {
              return;
            }
            if (localPath == null && bundlePath == null && urlString == null) {
              return;
            }
            final value = await ActionSheet.show(
              context: context,
              dataSource: [KeyValueModel(value: PublicText.saveImage)],
            );
            if (value != null) {
              // 请求权限
              final pass = await PermissionManager.requestPermission(
                permissionList: [
                  Permission.storage,
                  Permission.photos,
                ],
              );
              if (!pass) {
                if (!context.mounted) {
                  return;
                }
                PublicAlertDialog.show(
                    context: context,
                    title: await PermissionManager.isAndroidAPIBelow33()
                        ? PublicText.noStoragePermission
                        : PublicText.noPhotosVideoPermission,
                    message: PublicText.pleaseOpenPermission,
                    confirmText: PublicText.toSetting,
                    onConfirm: () {
                      openAppSettings();
                    });
                return;
              }

              final result = await FileManager.saveImageToGallery(
                localPath: localPath,
                bundlePath: bundlePath,
                urlString: urlString,
              );
              if (result) {
                Toast.show(PublicText.saveSuccess);
              }
            }
          },
          child: image(),
        ),
      ),
    );
  }

  Widget image() {
    if (stringIsNotEmpty(urlString)) {
      return PhotoView(
        backgroundDecoration: const BoxDecoration(color: Colors.transparent),
        initialScale: PhotoViewComputedScale.contained,
        minScale: PhotoViewComputedScale.contained * 1,
        maxScale: PhotoViewComputedScale.contained * 3,
        imageProvider: NetworkImage(urlString!),
      );
    }
    if (stringIsNotEmpty(localPath)) {
      return PhotoView(
        backgroundDecoration: const BoxDecoration(color: Colors.transparent),
        initialScale: PhotoViewComputedScale.contained,
        minScale: PhotoViewComputedScale.contained * (minScale ?? 1),
        maxScale: PhotoViewComputedScale.contained * (maxScale ?? 3),
        imageProvider: FileImage(File(localPath!)),
      );
    }
    if (stringIsNotEmpty(bundlePath)) {
      return PhotoView(
        backgroundDecoration: const BoxDecoration(color: Colors.transparent),
        initialScale: PhotoViewComputedScale.contained,
        minScale: PhotoViewComputedScale.contained * (minScale ?? 1),
        maxScale: PhotoViewComputedScale.contained * (maxScale ?? 3),
        imageProvider: AssetImage(bundlePath!),
      );
    }
    return const SizedBox();
  }
}
